﻿// Copyright 2014 Foundation Team
//  
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//   
//        http://www.apache.org/licenses/LICENSE-2.0
//   
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

#region 导入名称空间

using System;
using System.ServiceProcess;
using Topshelf;
using Topshelf.HostConfigurators;
using Topshelf.Runtime;

#endregion

namespace LightWork.Topshelf.Autofac
{
    /// <summary>
    /// 用户定义服务配置
    /// </summary>
    [Serializable]
    public class CustomerHostConfiguration
    {
        /// <summary>
        /// Windows服务宿主管理服务 构造器
        /// </summary>
        public CustomerHostConfiguration()
        {
            ServiceName = "EasyServer";
            InstanceName = "HostManagementService";

            ServiceDisplayName = "Windows服务宿主管理服务";
            ServiceDescription = "由Topshelf提供宿主环境的Windows服务，可在其上运行各种用户自定义托管服务";

            RunAs = ServiceAccount.LocalService;
            StartMode = HostStartMode.Automatic;
        }

        public HostConfigurator Configure(ref HostConfigurator hostCfg)
        {
            #region 设置运行模式

            switch (RunAs)
            {
                case ServiceAccount.LocalService:
                    hostCfg.RunAsLocalService();
                    break;
                case ServiceAccount.LocalSystem:
                    hostCfg.RunAsLocalSystem();
                    break;

                case ServiceAccount.NetworkService:
                    hostCfg.RunAsNetworkService();
                    break;

                case ServiceAccount.User:
                    hostCfg.RunAs(UserName, Password);
                    break;

                default:
                    hostCfg.RunAsPrompt();
                    break;
            }

            #endregion

            #region 设置启动模式

            switch (StartMode)
            {
                case HostStartMode.AutomaticDelayed:
                    hostCfg.StartAutomaticallyDelayed();
                    break;
                case HostStartMode.Disabled:
                    break;
                case HostStartMode.Manual:
                    hostCfg.StartManually();
                    break;

                default:
                    hostCfg.StartAutomatically();
                    break;
            }

            #endregion

            #region 设置服务基础信息

            hostCfg.SetServiceName(ServiceName);
            hostCfg.SetInstanceName(InstanceName);
            hostCfg.SetDescription(ServiceDescription);
            hostCfg.SetDisplayName(ServiceDisplayName);

            #endregion

            return hostCfg;
        }

        #region Propertities

        /// <summary>
        /// 服务用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 服务登陆密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 服务运行类别
        /// </summary>
        public ServiceAccount RunAs { get; set; }

        /// <summary>
        /// 服务启动模式
        /// </summary>
        public HostStartMode StartMode { get; set; }

        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; }

        /// <summary>
        /// 服务描述
        /// </summary>
        public string ServiceDescription { get; set; }

        /// <summary>
        /// 服务显示名称
        /// </summary>
        public string ServiceDisplayName { get; set; }

        /// <summary>
        /// 服务实例名称
        /// </summary>
        public string InstanceName { get; set; }

        #endregion
    }
}